package com.holly.unit.deform.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.holly.unit.deform.api.pojo.online.pojo.ColumnInfo;

import java.util.HashMap;
import java.util.Map;

public class JsonUtil {

    // 自定义页面过滤字段
    public  static String pageFilterFields = "id,create_time,create_by,update_time,update_by";

    public static Map<String, ColumnInfo> createDataTable(JSONArray jsonList) {
        if(jsonList.size() > 0){
            Map<String, ColumnInfo> map = new HashMap<>();
            jsonList.forEach(item ->createTable((JSONObject)item,map));
            return map;
        }
        return null;
    }


    /**
     * 类描述: 根据输入框生成数据类型
     *
     * @param item
     * @date 2022/1/15 14:25
     */
    private static String createTable(JSONObject item, Map sb) {
        String dataType = "";
        ColumnInfo columnInfo = new ColumnInfo();
        //
        if(item.getString("type").equals("imgupload") || item.getString("type").equals("file-upload") ||
                item.getString("type").equals("checkbox") || item.getString("type").equals("buttons") ||
                item.getString("type").equals("divider") || item.getString("type").equals("text")){
            ((JSONObject) item.get("options")).put("defaultValue","");
        }
        switch (item.getString("type")) {
            case "imgupload":
                sb.put(item.getString("model"),tableColumn(item,columnInfo,"varchar(500)"));
                break;
            case "file-upload":
                sb.put(item.getString("model"),tableColumn(item,columnInfo,"varchar(500)"));
                break;
            case "text":
                sb.put(item.getString("model"),tableColumn(item,columnInfo,"text"));
                break;
            case "textarea":
                sb.put(item.getString("model"),tableColumn(item,columnInfo,"text"));
                break;
            case "editor":
                sb.put(item.getString("model"),tableColumn(item,columnInfo,"text"));
                break;
            case "number":
                sb.put(item.getString("model"),tableColumn(item,columnInfo,"int(5)"));
                break;
            case "rate":
                sb.put(item.getString("model"),tableColumn(item,columnInfo,"int(5)"));
                break;
            case "slider":
                sb.put(item.getString("model"),tableColumn(item,columnInfo,"int(5)"));
                break;
            case "grid":
                item.getJSONArray("columns").forEach(gridList -> {
                    JSONArray list = ((JSONObject) gridList).getJSONArray("list");
                    if (list.size() > 0) {
                        list.forEach(grid -> {
                            createTable((JSONObject) grid,sb);
                        });
                    }
                });
                break;
            case "card":
                JSONArray list = item.getJSONArray("list");
                if (list.size() > 0) {
                    list.forEach(card -> {
                        createTable((JSONObject) card,sb);
                    });
                }
                break;
            case "tabs":
                item.getJSONArray("panes").forEach(tabsList -> {
                    JSONArray tabs = ((JSONObject) tabsList).getJSONArray("list");
                    if (tabs.size() > 0) {
                        tabs.forEach(t -> {
                            createTable((JSONObject) t,sb);
                        });
                    }
                });
                break;
            case "sub-table-design":
                item.getJSONArray("columns").forEach(subList -> {
                    JSONArray listSub = ((JSONObject) subList).getJSONArray("list");
                    if (listSub.size() > 0) {
                        listSub.forEach(sub -> {
                            createTable((JSONObject) sub,sb);
                        });
                    }
                });
                break;
            default:
                sb.put(item.getString("model"),tableColumn(item,columnInfo,"varchar(255)"));
                break;
        }
        return dataType;
    }

    private static ColumnInfo tableColumn(JSONObject item, ColumnInfo columnInfo, String s) {
        //默认值
        Object o = ((JSONObject) item.get("options")).get("defaultValue");
        if(o != null){
            columnInfo.setDbDefaultVal(o.toString());
        }
        //是否不能为空
        Object required = ((JSONObject) item.get("options")).get("required");
        if(required != null){
            if((Boolean)required){
                columnInfo.setNullAble(1);
            }else {
                columnInfo.setNullAble(0);
            }
        }else {
            columnInfo.setNullAble(0);
        }
        //字段注释
        columnInfo.setColComment(item.getString("name"));
        //数据库类型
        columnInfo.setColType(s);
        columnInfo.setColName(item.getString("model"));
        return columnInfo;
    }


    /**
     * 组装建表sql
     * @param dataTable
     * @param desformCode
     * @param desformName
     * @date 2022/1/15 14:25
     */
    public static String javaCreateTable(Map<String, ColumnInfo> dataTable, String desformCode, String desformName) {
        StringBuffer sb = new StringBuffer();
        sb.append("CREATE TABLE `"+ desformCode +"` (\n");
        sb.append("`id` varchar(32) NOT NULL COMMENT '主键',\n");
        for (ColumnInfo columnInfo : dataTable.values()) {
            String tableSql = createTableSql(columnInfo);
            sb.append(tableSql+" ,\n");
        }
        sb.append("`act_status` varchar(255) DEFAULT NULL COMMENT '流程状态',\n ");
        sb.append("PRIMARY KEY (`id`)\n");
        sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='"+desformName+"';");
        return sb.toString();
    }

    public static String createTableSql(ColumnInfo columnInfo) {
        String value = "";
        if (StrUtil.isEmpty(columnInfo.getDbDefaultVal()) || "text".equals(columnInfo.getColType())) {
            value = " DEFAULT NULL COMMENT ";
        } else {
            value = " DEFAULT " + judgeIsNumber(columnInfo.getDbDefaultVal()) + " COMMENT ";
        }
        return judgeNumber(columnInfo.getColName())+" "+ columnInfo.getColType() + " "+ value + judgeIsNumber(columnInfo.getColComment());
    }

    public static String judgeIsNumber(String text){
        return "'"+text+"'";
    }

    public static String judgeNumber(String text){
        return "`"+text+"`";
    }

    /**
     * 获取修改的表字段
     * */
    public static String getUpdateColumnSql(ColumnInfo columnInfo) {
        return " MODIFY COLUMN "+createTableSql(columnInfo)+ ";";
    }

    /**
     * 添加表字段
     * */
    public static String getAddColumnSql(ColumnInfo columnMeta) {
        return " ADD COLUMN "+createTableSql(columnMeta)+";";
    }

    /**
     * 获取删除的表字段
     * */
    public static String getDropColumnSql(String fieldName) {
        return " DROP COLUMN `"+fieldName+"`;";
    }

}
